package young.frame;

import young.Util.DBUtil;
import young.pojo.UserDO;
import young.service.UserService;
import young.service.impl.UserServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 转账功能
 */
public class Transfer extends JPanel implements ActionListener {
    private JButton returnBut; //返回
    private JButton confirm;  //确认
    private JTextField tarnMoney;//转账金额
    private JTextField tarnId;
    private Atm frame;
    private int sum = 0;

    public Transfer(Atm frame) {

        this.frame = frame;

        this.setLayout(null); //空布局

        this.setOpaque(false); //背景透明


        returnBut = new JButton("返回");
        returnBut.setBounds(350, 580, 100, 50);
        this.add(returnBut);
        returnBut.addActionListener(this);

        //确认按钮,确认取款金额
        confirm = new JButton("确认");
        confirm.setBounds(350, 500, 100, 50);
        this.add(confirm);
        confirm.addActionListener(this);

        tarnMoney = new JTextField(); //转账金额文本框
        tarnMoney.setBounds(320, 220, 100, 30);
        this.add(tarnMoney);
        tarnMoney.addActionListener(this);

        tarnId = new JTextField(); //转账户名文本框
        tarnId.setBounds(320, 260, 100, 30);
        this.add(tarnId);
        tarnId.addActionListener(this);


        JLabel label = new JLabel("转入金额：");
        label.setBounds(260, 220, 80, 30);
        this.add(label);

        JLabel label1 = new JLabel("转入账户：");
        label1.setBounds(260, 260, 80, 30);
        this.add(label1);


    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == returnBut) {
            frame.switchPanel("菜单");


        }

        if (e.getSource() == confirm) {  //转账
            String login = frame.getLoginUser().getId();
            String other = tarnId.getText(); //获取转入账户

            int money = 0;
            try {
                money = Integer.valueOf(tarnMoney.getText());
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "转账金额必须为数字，并不能为空");
                return;
            }
            UserService service = new UserServiceImpl();
            try {
                service.transferMoney(login, other, money); //转账业务
                JOptionPane.showMessageDialog(this, "转账成功");
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(this, exception.getMessage());
            }
            frame.switchPanel("菜单");

        }


    }
}










